<?php
// UltraMemo Project - GG_UpdateQuestion.php
//   update a question in a gdoc spreadsheet based on answer passed
// 11/18/09 initial creation - Scott Szretter

//require_once("Quiz_GetWkShtListFeed.php"); //function to get question from gdoc spreadsheet

if(isset($_POST['a']) && isset($_POST['q'])) //they are passing an answer
{
    //find the question by it's qid
    $GData->GDataQueryString=('qid="'.$_POST['q'].'"');
    $listFeed=$GData->getWkshtListFeed();
   // $listFeed = getWkshtListFeed($gdClient, $sheetKey, $worksheetKey, ('qid='.$_POST['q']));
    if ( count($listFeed->entries) > 0)
    {
        $row = $listFeed->entries[0];
        $rowData = $row->getCustom();
       
        
        foreach($rowData as $customEntry)
        {
            //store current value in to update array
            $updatedRowArray[$customEntry->getColumnName()] = $customEntry->getText();

            // *** LEITNER UPDATE LOGIC ***
            if ($customEntry->getColumnName()=="leitnerbox")
            {
                
                $leitnerbox = $customEntry->getText(); //get the current value
                if($_POST['a']=="1") //good answer and not in last box
                {
                    $leitnerbox++; //increment to next leitner box since it's good
                }
                else if($_POST['a']=="0") //bad answer
                {
                    if($leitnerbox>1) //if its 1, just leave it
                        $leitnerbox--;  //decrement the box #
                    else
                        $leitnerbox=1; //make sure leitnerbox is at least 1

                }
        
                switch ($leitnerbox) {
                case 1: //tomorrow
                    $nextReviewDate = strtotime("+1 day");
                    break;
                case 2: //2 days
                    $nextReviewDate = strtotime("+2 days");
                    break;
                case 3: //1 week
                    $nextReviewDate = strtotime("+1 week");
                    break;
                case ($leitnerbox > 3): //months
                    $months = "+" . ($leitnerbox - 3) ." months" ;
                    $nextReviewDate = strtotime($months);
                    break;
                default:
                    $nextReviewDate = strtotime("now"); //should not get here, set to today
                    break;
            }

            //update the leitner cell (update row array)
            $updatedRowArray['leitnerbox']=$leitnerbox;
            } // if column = leitnerbox
            // *** END LEITNER UPDATE LOGIC ***

        } // for each column (rowdata)

        //do the update
        $updatedRowArray['nextreviewdt']=date("m/d/Y H:i:s",$nextReviewDate);
        if($_POST['a']=="1") $updatedRowArray['good']++;
        if($_POST['a']=="0") $updatedRowArray['bad']++;
        $updatedRow = $GData->UpdateSheet($row, $updatedRowArray);
       
    } // count entries > 0
    else
    {
        // error here?
    } //else cound entries
} // isset qid and ans
?>